/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.is.city;

import java.awt.Dimension;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.UnsupportedLookAndFeelException;
import org.geotools.map.DefaultMapContext;
import java.io.IOException;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.FileDataStore;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import org.geotools.map.MapContext;
import org.geotools.map.MapLayer;
import org.geotools.referencing.crs.EPSGCRSAuthorityFactory;
import org.geotools.renderer.GTRenderer;
import org.geotools.styling.BasicLineStyle;
import org.geotools.swing.JMapPane;
import org.geotools.swing.action.MapAction;
import org.geotools.swing.action.ZoomOutAction;
import org.is.city.search.SearchEngine;
import org.is.city.webcam.WebCamLayer;
import org.is.city.webcam.WebCamMousePaneListener;
import org.is.city.webcam.WebCamStyle;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.NoSuchAuthorityCodeException;
import org.opengis.referencing.crs.CRSAuthorityFactory;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 *
 * @author oleg
 */
public class NewMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SearchEngine engine = new SearchEngine();
        try {
            
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
            try {
                //URL resource = NewMain.class.getClassLoader().getResource("META-INF/improvedmap/improved0.04.shp");
                //File sourceFile = new File(resource.toURI());
                File sourceFile = new File("improvedmap/improved004.shp");
                if (sourceFile == null) {
                    return;
                }
                CRSAuthorityFactory aDefault = EPSGCRSAuthorityFactory.getDefault();
                CoordinateReferenceSystem crs = aDefault.createCoordinateReferenceSystem("EPSG:32637");
                FileDataStore store = FileDataStoreFinder.getDataStore(sourceFile);
                SimpleFeatureSource featureSource = store.getFeatureSource();
                MapContext map = new MapContext();
                map.addLayer(featureSource, new BasicLineStyle());
                JMainFrame instance = new JMainFrame(new DefaultMapContext(crs));
                instance.enableLayerTable(true);
                instance.enableToolBar(true);
                JToolBar toolbar = instance.getToolBar();

                toolbar.addSeparator();
                
                
                instance.getMapPane().setCursorTool(new MyCursor(instance.getMapPane()));
                instance.initComponents();
                instance.setVisible(true);
                instance.setBounds(100, 100, 1000, 600);
                instance.setPreferredSize(new Dimension(1000, 700));
                //Добавляем mouse listeners
                JMapPane mapPane = instance.getMapPane();
             //mapPane.addMouseListener(new MyMouseAdapter(mapPane));
                MapLayer obninskLayer = new MapLayer(featureSource, new BasicLineStyle(), "Обнинск");
                //instance.getMapContext().addLayer(featureSource, new BasicLineStyle() );
                instance.getMapContext().addLayer(obninskLayer);
                GTRenderer renderer = instance.getMapPane().getRenderer();
                RenderingHints hints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
                renderer.setJava2DHints(hints);
                RenderingHints hints2 = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
                renderer.setJava2DHints(hints2);
                Map rendererParams = new HashMap();
                rendererParams.put("optimizedDataLoadingEnabled", new Boolean(true));
                renderer.setRendererHints(rendererParams);


/*
                WebCamFrame frame=new WebCamFrame(new URL("http://localhost:8888"), 1, 320, 240);
                frame.setVisible(true);
 *
 */
                File webcamfile=new File("improvedmap/webcams.shp");
                FileDataStore store_webcams = FileDataStoreFinder.getDataStore(webcamfile);
                MyStyle ms=new MyStyle(MyStyle.GeomType.POINT);
                WebCamLayer camLayer=new WebCamLayer(store_webcams.getFeatureSource(), WebCamStyle.createStyle(), "веб камеры");
                instance.getMapContext().addLayer(camLayer);
                WebCamMousePaneListener camMousePaneListener=new WebCamMousePaneListener(camLayer, mapPane);
                instance.getMapPane().addMouseListener(camMousePaneListener);
                instance.getMapPane().setDoubleBuffered(true);
            } catch (NoSuchAuthorityCodeException ex) {
                Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
            } catch (FactoryException ex) {
                Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedLookAndFeelException ex) {
            Logger.getLogger(NewMain.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
